'use strict';

/**
 * @ngdoc service
 * @name jgappApp.initEvents
 * @description
 * # initEvents
 * Service in the jgappApp.
 */
angular.module('jgappApp')
    .service('SearchService', function (DataService, PagingService, $timeout, localStorageService, openApi, $q, Modulemap) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        var SService = {}
        // 将流程查询配置的数据缓存起来
        SService.prefix = "searchModule_";
        // 首页提醒功能列表
        SService.getAllremind = function () {
            var defer = $q.defer();
            DataService.getData(openApi.reminderConfig).then(function (data) {
                data.forEach(function (item, index) {
                    //为二级分类添加viewname以及className
                    Modulemap.getviewData(item, item.formKey)
                });
                defer.resolve(data);
            })
            return defer.promise;
        }
        //获取一级菜单
        SService.getDataList = function () {
            var defer = $q.defer();
            var data = localStorageService.get(SService.prefix + "MainMenu");
            if (data) {
                $timeout(function () {
                    defer.resolve(data);
                }, 100)
            } else {
                DataService.getData(openApi.getsearchDataList).then(function (data) {
                    data.forEach(function (item, index) {
                        //为一级分类添加viewname以及className
                        Modulemap.getviewData(item, item.name)
                    });
                    localStorageService.set(SService.prefix + "MainMenu",data);
                    defer.resolve(data);
                })
            }
            return defer.promise;
        };
        //获取二级菜单
        SService.getSubDataList = function (id) {
            var defer = $q.defer();
            var param = {};
            param.functionId = id;
            param.noloader = true
            DataService.getData(openApi.getSubsearchDataList, param).then(function (data) {
                data.forEach(function (item, index) {
                    //为二级分类添加viewname以及className
                    Modulemap.getviewData(item, item.name)
                });

                defer.resolve(data);
            })
            return defer.promise;
        };
        //获取二级菜单的配置数据
        SService.getSubDataConfig = function (id) {
            var defer = $q.defer();
            var param = {};
            param.functionId = id;
            param.noloader = true
            var data = localStorageService.get(SService.prefix + id);
            if (data) {
                $timeout(function () {
                    defer.resolve(data);
                }, 100)
            } else {
                DataService.getData(openApi.getSubDataConfig, param).then(function (data) {
                    localStorageService.set(SService.prefix + id, data);
                    defer.resolve(data);
                })
            }
            return defer.promise;
        };
        // 获取列表
        SService.getList = function (vm, id, param) {
            var defer = $q.defer();
            SService.getSubDataConfig(id).then(function (data) {
                // 将过滤的数据暴漏出去
                var config = {};
                param = param || {};
                param.id = id;
                config.porlist = data.displayFields;
                vm.taskList = new PagingService(openApi.ip + data.urlAddress, param, config);
            })
            return defer.promise;
        }
        // 获取过滤下拉框数据
        SService.getFilterValues = function (url) {
            var defer = $q.defer();
            DataService.getData(openApi.ip + url).then(function (data) {
                defer.resolve(data);
            })
            return defer.promise;
        }
        return SService;
    })
    .service('IndexService', function (DataService, $timeout, Modulemap, openApi, $q, localStorageService) {
        // AngularJS will instantiate a singleton by calling "new" on this function
        // 过滤数据
        var IService = {}
        IService.prefix = "IndexModule_"
        var getCompleteData = function (data) {
            var dataObj = {
                dataArr: [],
                datalength: 0
            };
            data.forEach(function (item, index) {
                //计算二级分类长度，如果大于八则显示一级分类，否则直接显示二级分类
                //item表示一级分类，subitem表示二级分类
                dataObj.datalength += item.subcategory.length;
                //为一级分类添加viewname以及className
                Modulemap.getviewData(item, item.categoryType)
                item.subcategory.forEach(function (subitem, index) {
                    subitem = Modulemap.getviewData(subitem, subitem.iconUrl)
                    dataObj.dataArr.push(subitem);
                })

            });
            return dataObj
        }
        var getDatalist = function (data) {
            var datalist = []
            var dataObj;
            dataObj = getCompleteData(data);
            if (dataObj.datalength > 8) {
                datalist = data
            } else {
                datalist = dataObj.dataArr
            }
            return datalist

        }
        IService.getmenulist = function () {
            var defer = $q.defer();
            var data = localStorageService.get(IService.prefix + "dataIndex")
            if (data) {
                $timeout(function () {
                    defer.resolve(data);
                }, 100)
            } else {
                DataService.getData(openApi.category).then(function (data) {
                    var datalist = getDatalist(data);
                    localStorageService.set(IService.prefix + "dataIndex", datalist);
                    defer.resolve(datalist);
                })
            }
            return defer.promise;
        };

        return IService;
    });
